import base64
import json
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex



class PrpCrypt(object):
    def __init__(self, key, iv):
        self.key = key.encode('utf-8')
        self.mode = AES.MODE_CBC
        self.iv = iv.encode('utf-8')

    def pad_byte(self, b):
        '''
        1 先计算所传入bytes类型文本与16的余数
        2 在将此余数转成bytes 当然用0补位也可以
        3 已知了 余数 那么就用余数*被转成的余数，就得到了需要补全的bytes
        4 拼接原有文本和补位
        :param b: bytes类型的文本
        :return: 返回补全后的bytes文本
        '''
        bytes_num_to_pad = AES.block_size - (len(b) % AES.block_size)
        # python3 中默认unicode转码
        # 实际上byte_to_pad 就已经 将 数字转成了unicode 对应的字符  即使你的入参正好是16的倍数，那么bytes也是把列表整体的转码也是有值的
        # 后边解密的匿名函数 拿到最后一个数字后，就知道应该截取的长度，在反着切片就行了
        # 这样保证了数据的完整性
        byte_to_pad = bytes([bytes_num_to_pad])
        padding = byte_to_pad * bytes_num_to_pad
        padded = b + padding
        return padded

    def encrypt(self, text):
        '''
        1 先生成aes实例
        2 对传入的text转成bytes
        3 对传入的text补全
        4 调用encrypt 加密 得到密文
        5 先将密文转16进制，在将16进制用base64转码，然后在将得到的base64解码
        其实在步骤4 就已经完成了aes加密，我所在的公司加密比较复杂 ，需要的可以直接返回步骤4的值

        :param text:
        :return:
        '''
        cryptor = AES.new(self.key, self.mode, self.iv)
        text = text.encode('utf-8')
        text = self.pad_byte(text)
        self.ciphertext = cryptor.encrypt(text)
        # cryptbase64 = base64.b64encode(b2a_hex(self.ciphertext)).decode('utf8')
        # return cryptbase64
        cryptbase64 = base64.b64encode(self.ciphertext).decode('utf8')
        print(cryptbase64)
        return cryptbase64

    def decrypt(self, text):
        '''
        解密和加密的顺序是相反的
        1 定义匿名函数，去掉补位
        2 base64解码
        3 生成aes实例
        # 4 16进制转2进制
        5 使用decrypt解码  得到补全的bytes类型明文

        :param text:
        :return:  解密且去掉补位的明文
        '''
        def unpad(s): return s[:-ord(s[len(s) - 1:])]
        base64Str = base64.b64decode(text.encode('utf8'))
        cryptor = AES.new(self.key, self.mode, self.iv)
        # aesStr = cryptor.decrypt(a2b_hex(base64Str))
        aesStr = cryptor.decrypt(base64Str).decode('utf8')
        aesStr = unpad(aesStr)
        print(aesStr)
        return aesStr


if __name__ == '__main__':
    print('----')
    print('-----加密-----')
    pc2 = PrpCrypt('amvCpadghXNnORw74LG2QdT7pwKz9kci', '8229165556696540')
    #获取sign
    with open('sign.txt') as f:
        sign = f.read()
    s_test1 = sign
    #获取请求
    request = json.dumps({
        "accessToken": "QXFxUnI3eHYveWNnNDVWMkhyV2F2RzRac2ZSUTg5T3EwM1BYUnFHalBvU0JETjBNN3JGM3B6N3lWSkc4K0pWQm1tZklXZVZVTkMvOHFsdngrbkpSckhWM2R0TGwvU3R3SjV0bk4wV3VWN1E9",
        "android_id": "89b55ae86c63da6b",
        "imei": "866174491984528",
        "introduction": "V7HzWaKgyKmjwTkDZk5%2F9w%3D%3D",
        "isVM": "0",
        "loginType": "4",
        "mac": "08:00:27:2D:D8:96",
        "pid": "104725",
        "platform": "Android",
        "refreshToken": "Sm1SbGJQZzF5WDUyMkhVdUV2ZXFrcDNkK3BTTHRTS2FuOVplZS9WT0xjeTQ4aHdEK2FYRWtKMmVMN0NjT1RXNDlVRHMzVnlaRFZuY0pWdnJkT1hGUTBUVGJkMW9xbVF6SkRlRHlyYktSVjQ9",
        "signType": "RSA",
        "tel": "13815335078",
        "timestamp": "2021-04-14 10:41:46",
        "unit": "OPPO R11 Plus",
        "username": "UC191812",
        "version": "1.1",
        "sign": s_test1
    })
    #s_test = 'FK8GqM4uQsynsUwRR7J8KiITft9SXI2C8LZ22uLPTOX7iz1OT91j/s7tOBUszbFo6mNe6Ymx9IVMRU5p42Gt0dAGQ+XZnX2k1soLZCqD9fuutGzkyumkrCFbn7f2/heGszD4DU9MJLDgO2enYA5HjZRMgiMYV53BMG9Mex6wjfl7Mr/pfmTTc8Re8tmAKhm3dRhtYAbvroFVNceLjuzHBd0natKiN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